package cn.com.liuwenquan.flex.business.commands
{
	import mx.rpc.IResponder;
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	
	import cn.com.liuwenquan.flex.vo.User;
	import cn.com.liuwenquan.flex.events.LoginEvent;
	import cn.com.liuwenquan.flex.model.LoginDataModelLocater;
	import cn.com.liuwenquan.flex.business.Delegates.LoginDelegate;
	import cn.com.liuwenquan.flex.model.SessionModelLocator;
	
	import mx.controls.Alert;

	public class LoginCommand implements ICommand, IResponder
	{
		public function LoginCommand()
		{
		}

		public function execute(event:CairngormEvent):void
		{
			var lE:LoginEvent = LoginEvent(event);
			var user:User = lE.loginUser;
			//创建所需要的代理，连到Web端
			var lgoinService :LoginDelegate =  new LoginDelegate(this);
			//处理了代理的响应？
			lgoinService.authenticate(user);
			
		}
		
		public function result(event:Object):void
		{   
		    trace("-----验证成功，读取文件菜单--------:"+event.toString());
			var authUser:User = User(event.result);
			if(authUser.name == "无"){
				Alert.show("用户名或密码错误");
			}else{
				LoginDataModelLocater.getInstance().loginUser = authUser;
			    LoginDataModelLocater.getInstance().viewStackSelectedIndex=1;
			    LoginDataModelLocater.getInstance().xml = XML(authUser.menu_xml);
			    for(var i:int=0;i<XML(authUser.menu_xml).children().length();i++){
				    LoginDataModelLocater.getInstance().array.addItem(XML(authUser.menu_xml).children()[i].@label);
				    //trace("++++++++"+LoginDataModelLocater.getInstance().array.toString());
			    }
			    SessionModelLocator.getInstance().userId = authUser.userId;
//				MainStateManagers.setLoginName(authUser.loginName);
			}
		}
		
		public function fault(info:Object):void
		{
			Alert.show("DB连接错误致登录失败，请与管理员联系！");
		}
		
	}
}